package br.com.pontoclass.main;

import java.io.File;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Resource;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import br.com.pontoclass.patterns.PrimesListWriterBuilder;
import br.com.pontoclass.primes.DestinationList;
import br.com.pontoclass.primes.PrimesListWriter;


public class ListGenerator {

    private static final String THIS_BEANNAME = "main";
    private static final String CLASSPATH_SPRING_CONTEXT_XML_NAME = "classpath:spring-context.xml";
    private static final Logger LOGGER = Logger.getLogger(ListGenerator.class.getName());
    @Resource
    private DestinationList destinationList;
    
    /**
     * @param args
     */
    public static void main(String... args) {
        ListGenerator generator = new ClassPathXmlApplicationContext(CLASSPATH_SPRING_CONTEXT_XML_NAME).getBean(THIS_BEANNAME, ListGenerator.class);
        generator.generate(args);
    }

    private void generate(String[] args) {
        try{
            PrimesListWriter plw = PrimesListWriterBuilder.builder(new Scanner(new File(args[0])), destinationList)
                                                          .build(Integer.valueOf(args[1])); 
            plw.write();
        } catch(Exception e) {
            LOGGER.log(Level.SEVERE, String.format("Something went wrong during the execution: '%s'", e.getMessage()), e);
        }
    }
}